package com.lujianfei.powerinput.ui.noteList;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import com.lujianfei.powerinput.R;
import com.lujianfei.powerinput.base.BaseActivity;
import com.lujianfei.powerinput.database.NoteItemModel;
import com.lujianfei.powerinput.database.TemplateModel;
import com.lujianfei.powerinput.ui.noteList.base.NoteListAddContract;
import com.lujianfei.powerinput.ui.noteList.base.NoteListEditContract;
import com.lujianfei.powerinput.ui.noteList.presenter.NoteListAddPresenter;
import com.lujianfei.powerinput.ui.noteList.presenter.NoteListEditPresenter;
import com.lujianfei.powerinput.utils.AndroidBugFix;

import androidx.annotation.Nullable;

public class NoteListEditActivity extends BaseActivity implements NoteListEditContract.View{

    private LinearLayout container;
    private NoteListEditContract.Presenter presenter;
    private NoteItemModel noteItemModel;
    private Button bt_add_field;
    private AndroidBugFix androidBugFix;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_list_edit);
        androidBugFix = new AndroidBugFix(this);
        showTitleRightImage();
        setTitleRightImage(R.drawable.ic_ok);
        initView();

        presenter = new NoteListEditPresenter(this);
        parseIntent();
        if (noteItemModel!=null) {
            setTitleMiddleText(noteItemModel.getNoteName()+"信息字段编辑");
        }else{
            setTitleMiddleText("信息字段编辑");
        }
        presenter.init();
        initEvent();
    }

    private void initEvent() {
        bt_add_field.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.addRow();
            }
        });
    }

    @Override
    public void onTitleRightImageClick() {
        super.onTitleRightImageClick();
        presenter.onTitleRightImageClick();
    }

    private void parseIntent() {
        Intent intent = getIntent();
        noteItemModel = intent.getParcelableExtra("data");
        presenter.setData(noteItemModel);
    }

    private void initView() {
        container = findViewById(R.id.container);
        bt_add_field = findViewById(R.id.bt_add_field);
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    @Override
    public LinearLayout getContainer() {
        return container;
    }

    @Override
    public Activity currentActivity() {
        return this;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        presenter.onActivityResult(requestCode,resultCode,data);
    }

    @Override
    protected void onDestroy() {
        androidBugFix.onDestroy();
        super.onDestroy();
    }
}
